$(function () {

});
/**
 * 点击添加表结构
 */
function openAdd(){
    $("#propertyEditForm").empty();
    var html = "";
    html += '<input type="hidden" id="propertyId" value="0">';
    html += '<div class="row">';
    html += '<div class="col-sm-6 b-r">';

    html += '<div class="form-group">';
    html += '<label class="col-sm-3 control-label">中文描述</label>';
    html += '<div class="col-sm-9">';
    html += '<input class="form-control" id="CNName" name="CNName" type="text">';
    html += '</div>';
    html += '</div>';
    html += '<div class="hr-line-dashed"></div>';

    html += '<div class="form-group">';
    html += '<label class="col-sm-3 control-label">英文名称</label>';
    html += '<div class="col-sm-9">';
    html += '<input class="form-control" id="ENName" name="ENName" type="text">';
    html += '</div>';
    html += '</div>';
    html += '<div class="hr-line-dashed"></div>';

    html += '<div class="form-group">';
    html += '<label class="col-sm-3 control-label">长度</label>';
    html += '<div class="col-sm-9">';
    html += '<input class="form-control" id="length" name="length" type="number">';
    html += '</div>';
    html += '</div>';

    html += '</div>';

    html += '<div class="col-sm-6 b-r">';
    html += '<div class="form-group">';
    html += '<label class="col-sm-3 control-label">是否可为空</label>';
    html += '<div class="col-sm-9">';
    html += '<select class="form-control" id="isNull">';
    html += '<option value="1" selected="selected">Y</option>';
    html += '<option value="0">N</option>';
    html += '</select>';
    html += '</div>';
    html += '</div>';
    html += '<div class="hr-line-dashed"></div>';

    html += '<div class="form-group">';
    html += '<label class="col-sm-3 control-label">数据库类型</label>';
    html += '<div class="col-sm-9">';
    html += '<input class="form-control" id="dbType" name="dbType" type="text">';
    html += '</div>';
    html += '</div>';
    html += '<div class="hr-line-dashed"></div>';

    html += '<div class="form-group">';
    html += '<label class="col-sm-3 control-label">代码类型</label>';
    html += '<div class="col-sm-9">';
    html += '<input class="form-control" id="codeType" name="codeType" type="text">';
    html += '</div>';
    html += '</div>';
    html += '</div>';

    html += '<div class="col-sm-6 b-r">';
    html += '<div class="col-sm-9">';
    html += '<button type="button" class="btn btn-info " onclick="editPropert()" id="ensure">';
    html += '<i class="fa fa-check"></i>&nbsp;提交';
    html += '</button>&nbsp;';
    html += '<button type="button" class="btn btn-danger " onclick="closePropert()" id="cancel">';
    html += '<i class="fa fa-eraser"></i>&nbsp;取消';
    html += '</button>';
    html += '</div>';
    html += '</div>';
    html += '</div>';
    $("#propertyEditForm").append(html);
};
/**
 * 点击编辑虚拟表结构
 */
function openEdit(projectTableProperty){
    $("#propertyEditForm").empty();
    var str = projectTableProperty.substr(1,projectTableProperty.length-2);
    var json = eval("("+toArray(str)+")");
    var html = "";
    html += '<input type="hidden" id="propertyId" value="' + json['id'] + '">';
    html += '<div class="row">';
    html += '<div class="col-sm-6 b-r">';

    html += '<div class="form-group">';
    html += '<label class="col-sm-3 control-label">中文描述</label>';
    html += '<div class="col-sm-9">';
    html += '<input class="form-control" id="CNName" name="CNName" type="text" value="' + json['CN_name'] + '">';
    html += '</div>';
    html += '</div>';
    html += '<div class="hr-line-dashed"></div>';

    html += '<div class="form-group">';
    html += '<label class="col-sm-3 control-label">英文名称</label>';
    html += '<div class="col-sm-9">';
    html += '<input class="form-control" id="ENName" name="ENName" type="text" value="' + json['EN_name'] + '">';
    html += '</div>';
    html += '</div>';
    html += '<div class="hr-line-dashed"></div>';

    html += '<div class="form-group">';
    html += '<label class="col-sm-3 control-label">长度</label>';
    html += '<div class="col-sm-9">';
    html += '<input class="form-control" id="length" name="length" type="number" value="' + json['length'] + '">';
    html += '</div>';
    html += '</div>';

    html += '</div>';

    html += '<div class="col-sm-6 b-r">';
    html += '<div class="form-group">';
    html += '<label class="col-sm-3 control-label">是否可为空</label>';
    html += '<div class="col-sm-9">';
    html += '<select class="form-control" id="isNull">';
    if(json['isNull'] == 'Y'){
        html += '<option value="1" selected="selected">Y</option>';
        html += '<option value="0">N</option>';
    }else{
        html += '<option value="1" >Y</option>';
        html += '<option value="0" selected="selected">N</option>';
    }
    html += '</select>';
    html += '</div>';
    html += '</div>';
    html += '<div class="hr-line-dashed"></div>';

    html += '<div class="form-group">';
    html += '<label class="col-sm-3 control-label">数据库类型</label>';
    html += '<div class="col-sm-9">';
    html += '<input class="form-control" id="dbType" name="dbType" type="text" value="' + json['db_type'] + '">';
    html += '</div>';
    html += '</div>';
    html += '<div class="hr-line-dashed"></div>';

    html += '<div class="form-group">';
    html += '<label class="col-sm-3 control-label">代码类型</label>';
    html += '<div class="col-sm-9">';
    html += '<input class="form-control" id="codeType" name="codeType" type="text" value="' + json['code_type'] + '">';
    html += '</div>';
    html += '</div>';
    html += '</div>';

    html += '<div class="col-sm-6 b-r">';
    html += '<div class="col-sm-9">';
    html += '<button type="button" class="btn btn-info " onclick="editPropert()" id="ensure">';
    html += '<i class="fa fa-check"></i>&nbsp;提交';
    html += '</button>&nbsp;';
    html += '<button type="button" class="btn btn-danger " onclick="closePropert()" id="cancel">';
    html += '<i class="fa fa-eraser"></i>&nbsp;取消';
    html += '</button>';
    html += '</div>';
    html += '</div>';
    html += '</div>';
    $("#propertyEditForm").append(html);
};
//返回拼好json格式的字符串
function toArray(str)
{
  var list = str.split(",");
  var myStr = "{";
  for(var i=0;i<list.length;i++)
  {
    try{
      var keys = list[i].split("=");
      var key = keys[0];
      var value= keys[1];
      if(i>0)
      {
        myStr += ",";
      }
      myStr += "\""+key.trim()+"\":\""+value.trim()+"\"";
    }catch(e)
    {
      continue;
    }
  }
  myStr += "}";
  return myStr;
}
//替换掉字符串中头尾的空格
function Trim(str){
    str = str.substr(1,str.length-2);
}

function closePropert(){
    $("#propertyEditForm").empty();
}


function editPropert(){
    var id = $('#id').val();
    var propertyId = $('#propertyId').val();
    var CNName = $('#CNName').val();
    var ENName = $('#ENName').val();
    var length = $('#length').val();
    var isNull = $('#isNull').val();
    var dbType = $('#dbType').val();
    var codeType = $('#codeType').val();
    if (id == undefined || id.length == 0 || id.replace(/(^s*)|(s*$)/g, "").length == 0) {
        alert('表id不能为空');
        return;
    }
    if (propertyId == undefined || propertyId.length == 0 || propertyId.replace(/(^s*)|(s*$)/g, "").length == 0) {
        alert('属性id不能为空');
        return;
    }
    if (CNName == undefined || CNName.length == 0 || CNName.replace(/(^s*)|(s*$)/g, "").length == 0) {
        alert('中文描述不能为空');
        return;
    }
    if (ENName == undefined || ENName.length == 0 || ENName.replace(/(^s*)|(s*$)/g, "").length == 0) {
        alert('英文名称不能为空');
        return;
    }
    if (length == undefined || length.length == 0 || length.replace(/(^s*)|(s*$)/g, "").length == 0) {
        alert('长度不能为空');
        return;
    }
    if (isNull == undefined || isNull.length == 0 || isNull.replace(/(^s*)|(s*$)/g, "").length == 0) {
        alert('是否可为空不能为空');
        return;
    }
    if (dbType == undefined || dbType.length == 0 || dbType.replace(/(^s*)|(s*$)/g, "").length == 0) {
        alert('数据库类型不能为空');
        return;
    }
    if (codeType == undefined || codeType.length == 0 || codeType.replace(/(^s*)|(s*$)/g, "").length == 0) {
        alert('代码类型不能为空');
        return;
    }
    var operation = function () {
        var ajax = new $ax(Feng.ctxPath + "/projectTable/edit_property", function (data) {
            Feng.success("编辑成功!");
            window.location.reload();
        }, function (data) {
            Feng.error("编辑失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id", id);
        ajax.set("propertyId", propertyId);
        ajax.set("CNName", CNName);
        ajax.set("ENName", ENName);
        ajax.set("length", length);
        ajax.set("isNull", isNull);
        ajax.set("dbType", dbType);
        ajax.set("codeType", codeType);
        ajax.start();
    };
    Feng.confirm("确定编辑", operation);
}

function generatorCode(){
    var id = $('#id').val();
    var operation = function () {
        var ajax = new $ax(Feng.ctxPath + "/projectTable/generator_code", function (data) {
            Feng.success("生成成功!");
            Documents.table.refresh();
        }, function (data) {
            Feng.error("生成失败!" + data.responseJSON.message + "!");
        });
        ajax.set("projectTableId", id);
        ajax.start();
    };
    Feng.confirm("确定生成", operation);
}